﻿using System.Collections.Generic;
using System.Linq;
using Truextend.Common.Data;
using Truextend.Training.Business.Dtos;
using Truextend.Training.Business.Interfaces;
using Truextend.Training.Model.Entities;

namespace Truextend.Training.Business
{
    public class CourseManager : Repository<Course>, ICourseManager
    {
        public CourseManager(IUnitOfWork unitOfWork): base(unitOfWork)
        {
            
        }
                
        public IList<Course> GetAllCourses()
        {
            return this.SelectAll();
        }

        public IList<Course> GetAllCoursesByUser(int userId)
        {
            var context = (TrainingEntities)Context;
            var info = from a in context.Courses
                       from i in a.CourseInstances
                       from uc in i.UserCourses
                       where uc.UserId == userId
                       select a;
            return info.ToList();
        }

        public IList<CourseInstructorDto> GetCourseInstructorByUser(int userId)
        {
            return null;
        }

        public Course GetCourseById(long courseId)
        {
            var context = (TrainingEntities)Context;
            Course course = context.Courses.First(i => i.CourseId == courseId);
            return course;                        
        }
        
        public void AddCourse(Course course)
        {
            this.Add(course);
        }

        public void EditCourse(Course course)
        {
            Course courseUpdate = this.GetCourseById(course.CourseId);
            
            courseUpdate.Name = course.Name;
            courseUpdate.Category = course.Category;
            courseUpdate.DirectedTo = course.DirectedTo;
            courseUpdate.Prerequisites = course.Prerequisites;
            courseUpdate.Contents = course.Contents;
        }

        public void DeleteCourse(long courseId)
        {
            Course courseDelete = this.GetCourseById(courseId);
            this.Delete(courseDelete);
        }
    }
}
